package NWish
{
	import NWish.Interface.IApp;
	import NWish.properties.object3ds.*;
	
	import away3d.audio.Sound3D;
	import away3d.cameras.*;
	import away3d.containers.*;
	import away3d.core.base.Object3D;
	import away3d.entities.*;
	import away3d.lights.*;
	import away3d.primitives.*;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import org.aswing.AsWingManager;


	// -swf-version=13
	public class NWishEditor extends Sprite implements IApp
	{
		protected var _Plugins = new Object();
		
		private var _notifyNWEventEnable:Boolean = true;
		
		private var _fileSystem;
		private var _away3d;
		
		private var _kbEventDispatcher:EventDispatcher = new EventDispatcher();
		
		public function NWishEditor()
		{
			//
			super();
			
			//
			//_stageWidth = stage.stageWidth;
			//_stageHeight = stage.stageHeight;
			
			//
			this.addEventListener(MouseEvent.MOUSE_UP,		onMouseUp);
			var _this = this;
			function onMouseUp(e:Event)
			{
				//trace(e.target,e.target.parent);
				if(e.target.parent is View3D)
					stage.focus = _this;
			}
			
			this.addEventListener(KeyboardEvent.KEY_DOWN,	onKeyDown);
			this.addEventListener(KeyboardEvent.KEY_UP,		onKeyUp);
			function onKeyDown(e:Event)
			{
				if(stage.focus == _this)
					_kbEventDispatcher.dispatchEvent(e);
			}
			function onKeyUp(e:Event)
			{
				if(stage.focus == _this)
					_kbEventDispatcher.dispatchEvent(e);
			}

			//
			AsWingManager.initAsStandard(this);
			
			
			//
			_Plugins['Logger'] 					= new Logger(this);
			
			//
			_away3d								= new Away3d(this);
			_Plugins['Away3d'] 					= _away3d;
			
			_Plugins['MenuBar'] 				= new MenuBar(this);
			
			_fileSystem 						= new FileSystemEdit(this);
			_Plugins['FileSystem'] 				= _fileSystem;
			
			_Plugins['SceneTree'] 				= new SceneTree(this);
			_Plugins['ObjectExplorer'] 			= new ObjectExplorer(this);
			_Plugins['PropertiesExplorer'] 		= new PropertiesExplorer(this);
			_Plugins['Selector'] 				= new Selector(this);
			_Plugins['TransformEdit'] 			= new TransformEdit(this);
			_Plugins['UndoManager'] 			= new UndoManager(this);
			_Plugins['CameraHDController'] 		= new CameraHDController(this);
			
			//
			_Plugins['Logger']					.init();
			_Plugins['Away3d']					.init();
			_Plugins['MenuBar']					.init();
			_Plugins['FileSystem']				.init();
			_Plugins['SceneTree']				.init();
			_Plugins['ObjectExplorer']			.init();
			_Plugins['PropertiesExplorer']		.init();
			_Plugins['Selector']				.init();
			_Plugins['TransformEdit']			.init();
			_Plugins['UndoManager']				.init();
			_Plugins['CameraHDController']		.init();
			
			
			//
			Scene3DProperties.addPropertiesType(DirectionalLight		,DirectionalLightProperties);
			Scene3DProperties.addPropertiesType(PointLight				,PointLightProperties);
			Scene3DProperties.addPropertiesType(Camera3D				,Camera3DProperties/*					,'Camera'*/);
			Scene3DProperties.addPropertiesType(Cube					,CubeProperties);
			Scene3DProperties.addPropertiesType(Sphere					,SphereProperties);
			Scene3DProperties.addPropertiesType(Plane					,PlaneProperties);
			Scene3DProperties.addPropertiesType(RegularPolygon			,RegularPolygonProperties);
			Scene3DProperties.addPropertiesType(Cone					,ConeProperties);
			Scene3DProperties.addPropertiesType(Cylinder				,CylinderProperties);
			Scene3DProperties.addPropertiesType(SkyBox					,SkyBoxProperties);
			Scene3DProperties.addPropertiesType(Sprite3D				,Sprite3DProperties/*					,'Sprite'*/);
			Scene3DProperties.addPropertiesType(Mesh					,MeshLoaderProperties);
			Scene3DProperties.addPropertiesType(Sound3D					,Sound3DProperties);
			Scene3DProperties.addPropertiesType(WireframeAxesGrid		,function (...v) {return null;});
			Scene3DProperties.addPropertiesType(WireframeCube			,function (...v) {return null;});
			Scene3DProperties.addPropertiesType(WireframeGrid			,function (...v) {return null;});
			Scene3DProperties.addPropertiesType(WireframePlane			,function (...v) {return null;});
			Scene3DProperties.addPropertiesType(WireframeSphere			,function (...v) {return null;});
			Scene3DProperties.addPropertiesType(WireframePrimitiveBase	,function (...v) {return null;});
			Scene3DProperties.addPropertiesType(ObjectContainer3D		,ObjectContainer3DProperties);
			Scene3DProperties.addPropertiesType(Object3D				,function (...v) {throw new Error();});
			//Scene3DProperties.addPropertiesType('material'				,function (...v) {return null;});
			Scene3DProperties.addPropertiesType(Object					,function (...v) {throw new Error();});
			//addPropertiesType(,);
			
			
			//
			_Plugins['FileSystem'].newScene();
			
		}
		
		public function stageFocus()
		{
			stage.focus = this;
		}
		
		public function getPlugin(name:String)
		{
			return _Plugins[name];
		}
		
		public function get isEditor()
		{
			return true;
		}
		
		/*public function get ex()
		{
			return this;
		}*/
		
		public function get rootSprite():Sprite
		{
			return this;
		}
		
		
		public function get notifyNWEventEnable()
		{
			return _notifyNWEventEnable;
		}
		public function set notifyNWEventEnable(enable)
		{
			_notifyNWEventEnable = enable;
		}
		
		override public function dispatchEvent(event:Event):Boolean
		{
			if(event is NWEvent)
			{
				if(_notifyNWEventEnable)
					return super.dispatchEvent(event);
				
				return false;
			}

			return super.dispatchEvent(event);
		}
		

		public function get stageWidth()
		{
			return stage.stageWidth;
		}
		public function get stageHeight()
		{
			return stage.stageHeight;
		}
		
		//
		public function get fileSystem() : FileSystem
		{
			return _fileSystem;
		}
		
		public function get away3d() : Away3d
		{
			return _away3d;
		}
		
		
		public function get KBEventDispatcher():EventDispatcher
		{
			return _kbEventDispatcher;
		}
		
		public function get NWEventDispatcher():EventDispatcher
		{
			return this;
		}
		
		/*public function dispatchSkillEvent(skill_id, actor, x, z) : Boolean
		{
			return false;
		}*/
	}
}








